package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.ArticleDTO;
import com.sky.dto.ArticlePageDTO;
import com.sky.dto.ArticleStatus;
import com.sky.dto.ArticleUpdateDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ArticleService;
import com.sky.vo.ArticleUpdateVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("AdminArticleController")
@RequestMapping("/admin/article")
@Tag(name = "文章相关接口")
public class ArticleController {


    private final ArticleService articleService;

    @Autowired
    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }



    /**
     * 新增文章
     */
    @PostMapping("/add")
    @Operation(summary = "新增文章")
    public Result add(@RequestBody ArticleDTO articleDTO) {
        log.info("新增文章");
        articleService.add(articleDTO);
        return Result.success();
    }

    /**
     * 分页查询文章
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询文章")
    public Result<PageResult> page(ArticlePageDTO articlePageDTO) {
        log.info("分页查询文章：{}",articlePageDTO);
        PageResult pageResult = articleService.page(articlePageDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除文章")
    public Result delete(@RequestParam Long id) {
        log.info("删除文章：{}",id);
        articleService.delete(id);
        return Result.success();
    }

    /**
     * 修改文章
     */
    @PutMapping("/update")
    @Operation(summary = "修改文章")
    public Result update(@RequestBody ArticleUpdateDTO articleUpdateDTO) {
        log.info("修改文章：{}",articleUpdateDTO);
        articleService.updateArticle(articleUpdateDTO);
        return Result.success();
    }

    /**
     * 根据id查询文章
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询文章")
    public Result getById(@PathVariable Long id) {
        log.info("根据id查询文章：{}",id);
        ArticleUpdateVO articleUpdateVO = articleService.getById(id);
        return Result.success(articleUpdateVO);
    }

    /**
     * 根据id修改文章状态
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "根据id修改文章状态")
    public Result changeStatus(@RequestBody ArticleUpdateDTO articleUpdateDTO) {
        log.info("根据id修改文章状态：{}",articleUpdateDTO);
        articleService.changeStatus(articleUpdateDTO);
        return Result.success();
    }

    /**
     * 统计文章数量
     */
    @GetMapping("/count")
    @Operation(summary = "统计文章数量")
    public Result<Long> count() {
        log.info("统计文章数量");
        Long count = articleService.countArticle();
        return Result.success(count);
    }




}
